/*
聊天功能界面
*/
var confige = require("confige");

cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        chatItem:{
            default:null,
            type:cc.Node
        },
        faceItem:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.yOffset = 10;
        this.maxHeight = 420;
        this.curHeight = 0;
        this.itemCount = 0;
        this.itemMax = 10;
        this.itemList = [];
        this.mainNode = this.node.getChildByName("mainNode");
        this.mainContent = this.mainNode.getChildByName("chatView").getChildByName("view").getChildByName("content");
        this.chatView = this.mainNode.getChildByName("chatView")
        this.chatEdit = this.mainNode.getChildByName("chatEdit").getComponent("cc.EditBox");

        // this.faceFrameMap = {};
        this.faceNode = this.node.getChildByName("faceNode");
        console.log(this.faceNode);
        for(var i=1;i<=12;i++)
            confige.faceFrameMap[i] = this.faceNode.getChildByName(""+i).getComponent("cc.Sprite").spriteFrame;

        this.faceSelect = this.mainNode.getChildByName("faceSelect");
        this.quickSelect = this.mainNode.getChildByName("quickSelect");
        this.btnQuick = this.mainNode.getChildByName("btnQuick").getComponent("cc.Toggle");
        this.btnFace = this.mainNode.getChildByName("btnFace").getComponent("cc.Toggle");
        this.btnChat = this.mainNode.getChildByName("btnChat").getComponent("cc.Toggle");
        this.showFaceNode = false;
        this.showQuickNode = false;
        this.showChatNode = false;
        this.isInit = true;
        this.btnShowchatNode();
    },

    addChat:function(str){
        if(this.itemCount >= this.itemMax)
            this.deleteItem();

        var newItem = cc.instantiate(this.chatItem);
        this.itemList[this.itemCount] = newItem;
        this.mainContent.addChild(newItem);
        var curStr = newItem.getComponent("cc.RichText");
        curStr.string = str;
        var newHeight = newItem.height + 10;
        for(var i in this.itemList)
            this.itemList[i].y = this.itemList[i].y + newHeight;
        newItem.y = 0;
        newItem.active = true;

        this.curHeight += newHeight;
        this.itemCount ++;
    },

    addFace:function(str,faceID){
        if(this.itemCount >= this.itemMax)
            this.deleteItem();

        var newItem = cc.instantiate(this.faceItem);
        this.itemList[this.itemCount] = newItem;
        this.mainContent.addChild(newItem);
        var curStr = newItem.getComponent("cc.RichText");
        curStr.string = str;
        var face = newItem.getChildByName("face").getComponent("cc.Sprite");
        face.spriteFrame = confige.faceFrameMap[faceID];
        var newHeight = newItem.height + 10;
        for(var i in this.itemList)
            this.itemList[i].y = this.itemList[i].y + newHeight;
        newItem.y = 0;
        newItem.active = true;        
        this.curHeight += newHeight;
        this.itemCount ++;
    },

    deleteItem:function(){
        this.curHeight -= this.itemList[this.itemMax-1].height;
        this.itemList[0].destroy();
        this.itemList.shift();
        this.itemCount--;
    },

    btnChatSend:function(){
        var curChatStr = this.chatEdit.string;
        console.log("btnChatSend====="+curChatStr);
        var cleanEdit = function(){
            cleanEdit.edit.string = "";
        };
        cleanEdit.edit = this.chatEdit;
        if(curChatStr != "")
            pomelo.clientSend("say",{"msg": {"sayType":2, "string": curChatStr}},cleanEdit,cleanEdit);
        this.hideLayer();
    },

    btnQuickSend:function(event, customEventData){
        var index = parseInt(customEventData);
        pomelo.clientSend("say",{"msg": {"sayType":1, "index": index,"sex":confige.curSex}});
        this.showQuickNode = false;
        this.quickSelect.active = false;
        this.btnQuick.isChecked = false;
        this.hideLayer();
    },

    btnFaceSend:function(event, customEventData){
        var index = parseInt(customEventData);
        pomelo.clientSend("say",{"msg": {"sayType":0, "index": index}});
        this.showFaceNode = false;
        this.faceSelect.active = false;
        this.btnFace.isChecked = false;
        this.hideLayer();
    },

    btnShowFaceNode:function(){
        if(this.showFaceNode)
        {
            this.btnFace.isChecked = true;
            return;
        }
        this.chatView.active = false
        this.showChatNode = false;
        this.showQuickNode = false;
        this.quickSelect.active = false;
        this.btnQuick.isChecked = false;
        this.btnChat.isChecked = false;
        this.showFaceNode = true;
        this.faceSelect.active = true;
        // if(this.showFaceNode)
        // {
        //     this.showFaceNode = false;
        //     this.faceSelect.active = false;
        // }else{
        //     this.showFaceNode = true;
        //     this.faceSelect.active = true;
        // }
    },
    btnShowQuickNode:function(){
        if(this.showQuickNode)
        {
            console.log("11111111111111111")
            this.btnQuick.isChecked = true;
            return;
        }
        console.log("11111111111111111")
        this.chatView.active = false
        this.showChatNode = false;
        this.showFaceNode = false;
        this.faceSelect.active = false;
        this.btnFace.isChecked = false;
        this.btnChat.isChecked = false;
        this.showQuickNode = true;
        this.quickSelect.active = true;
        // if(this.showFaceNode)
        // {
        //     this.showFaceNode = false;
        //     this.faceSelect.active = false;
        //     this.btnFace.isChecked = false;
        // }
        // if(this.showQuickNode)
        // {
        //     this.showQuickNode = false;
        //     this.quickSelect.active = false;
        // }else{
        //     this.showQuickNode = true;
        //     this.quickSelect.active = true;
        // }
    },
    btnShowchatNode:function()
    {
        if(this.showChatNode)
        {
            this.btnChat.isChecked = true;
            return;
        }
        this.chatView.active = true
        this.showChatNode = true;
        this.showFaceNode = false;
        this.faceSelect.active = false;
        this.btnFace.isChecked = false;
        this.btnQuick.isChecked = false;
        this.btnChat.isChecked = true;
        this.showQuickNode = false;
        this.quickSelect.active = false;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
        this.showChatNode = false;
        this.btnShowchatNode();
    },

    hideLayer:function(){
        this.node.active = false;
        this.parent.btn_quickSay.active = true;
    },
});
